import { useUser } from '@/store/modules/user'
import { DictDataModel } from '@/api/model/dictModel'

/**
 * 字典数据
 * @param code 字典标识
 * @returns
 */
export function useDictData(code: string | number) {
  if (!code) return ref([])
  const { dictMap } = storeToRefs(useUser())
  const data = ref<DictDataModel[]>([])

  watch(
    dictMap,
    (dict) => {
      data.value = dict[code] || []
    },
    {
      immediate: true
    }
  )
  return data
}

/**
 * 字典集合数据
 * @param codeList 字典标识数组
 * @returns
 */
export function useDictListData(codeList: Array<string | number> = []) {
  const userStore = useUser()
  const data = ref<Array<DictDataModel[]>>([])

  codeList.forEach((code) => {
    const dict = userStore.dictMap[code] || []
    data.value.push(dict)
  })

  return data
}

/*
 * 字典回显
 * @param {array} data 字典数据
 * @param {any} value 字典的值
 * @param {string} labelCode label的标识
 * @param {string} valueCode value的标识
 */
export function useDictDisplay(
  data: any[],
  value: string | number | boolean,
  labelCode = 'label',
  valueCode = 'value'
) {
  const findItem = data?.find((item) => item[valueCode] == value)
  return findItem ? findItem[labelCode] : ''
}
